<template>
  <div>
    <el-card class="card">
      <!-- 搜索 -->
      <SearchForm :searchConfig="searchConfig" :form_data.sync="listQuery" @search="search" @reset="reset"
        v-if="searchConfig.length > 0"></SearchForm>

      <!-- 列表 -->
      <Table v-loading="loading" ref="Table" :data="tableData" :config="tableConfig" :handlerBtnConfig="handlerBtnConfig">
        <template slot="operation" slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleInfo(scope.row.id)">详情</el-button>
        </template>
      </Table>

      <Pagination :currentPage="listQuery.pageNum" :pageSize="listQuery.pageSize" :total="total" @change="change" />
    </el-card>
    <!-- 编辑与添加 -->
    <order-form ref="addForm" @saveAfter="saveAfter" />
    <!-- 导入信息-->>
    <el-dialog title="订单信息导入" :visible.sync="dialogVisible" width="30%">
      <el-form ref="importFormRef">
        <el-form-item label="请选择Excel文件">
            <el-upload
              name="file"
              action="string"
              accept=".xlsx,.xls"
          		:http-request="improtExcleOrder"
          		ref="upload"
          		:limit="fileLimit"
          		:on-remove="handleRemove"
          		:file-list="fileList"
          		:on-exceed="handleExceed"
          		:before-upload="beforeUpload"
          		:show-file-list="false"
          		>
          		<el-button size="small" type="primary">点击上传</el-button>
          	</el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import SearchForm from '@/common/SearchForm'
import Table from '@/common/Table'
import Pagination from '@/common/Pagination'
import OrderForm from './orderForm'
import { tableConfig, searchConfig } from './orderConfig'
import { getOrderList, exportOrder, importOrder } from '@/api/sys/order'

export default {
  components: {
    SearchForm,
    Table,
    Pagination,
    OrderForm
  },
  data() {
    return {
      loading: false,
      tableData: [],
      filename: '',
      autoWidth: true,
      bookType: 'xlsx',
      tableConfig: tableConfig(),
      searchConfig: searchConfig(),
      // 附件数量限制
      fileLimit: 1,
      fileList: [],
      dialogVisible: false,
      handlerBtnConfig: [
        {
          label: "导入订单",
          type: "primary",
          size: "mini",
          round: false,
          plain: false,
          icon: "el-icon-upload2",
          handler: () => {
            this.dialogVisible = true
          }
        },
        {
          label: "导出订单",
          type: "primary",
          size: "mini",
          round: false,
          plain: false,
          icon: "el-icon-download",
          handler: () => {
            this.handleExportOrder()
          }
        },
      ],
      listQuery: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  created() {
    this.handleGetList()
  },
  methods: {
    search() {
      this.listQuery.pageNum = 1
      this.handleGetList()
    },
    reset() {
      this.listQuery = {
        pageNum: 1,
        pageSize: 10
      }
    },
    // 获取列表
    handleGetList() {
      getOrderList(this.listQuery).then(res => {
        if (res.status === 200) {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    // 分页事件
    change(pageParam) {
      this.listQuery.pageNum = pageParam.currentPage
      this.listQuery.pageSize = pageParam.pageSize
      this.handleGetList()
    },
    // 详情
    handleInfo: function (id) {
      this.$refs.addForm.update(id)
    },
    // 新增
    saveAfter() {
      // this.handleGetList()
    },

    // 导出Excle
    handleExportOrder: function () {
      exportOrder().then((res) => {
        if (res.data) {
          this.$message.success('数据导出成功')
        } else {
          this.$message.success('数据导出失败')
        }
        this.listQuery.pageNum = 1
        this.handleGetList()
      });
    },

    // 导入Excle
    improtExcleOrder(item) {
      //上传文件的需要formdata类型;所以要转
      let FormDatas = new FormData()
      FormDatas.append('file', item.file);
      importOrder(FormDatas).then((res) => {
        if (res.data) {
          this.$message.success('数据导入成功')
          this.dialogVisible = false
          this.listQuery.pageNum = 1
          this.handleGetList()
        } else {
          this.$message.error('数据导入失败')
        }

        this.$refs.importFormRef.resetFields()//清除表单信息
        this.$refs.upload.clearFiles()//清空上传列表
        this.fileList = []//集合清空
        this.dialogVisible = false//关闭对话框
      })
    },

    // 上传对于一个文件
    handleExceed() {
      this.$message.warning('只能选取一个文件')
    },

    handleRemove() {},

    beforeUpload(file){
      const extension = file.name.split(".")[1] === "xls";
      const extension2 = file.name.split(".")[1] === "xlsx";
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!extension && !extension2) {
        this.$message({
          message: '上传模板只能是 xls、xlsx格式!',
          type: 'error'
        });
      }
      if (!isLt2M) {
        console.log("上传模板大小不能超过 10MB!");
        this.$message({
          message: '上传模板大小不能超过 10MB!',
          type: 'error'
        });
      }
      return extension || extension2 || isLt2M;
    },

  }
}
</script>

<style scoped>
.card {
  height: 100%;
  margin: 15px;
}
</style>

 